import publicForm from "@/components/public/el-forms.vue"; //弹框form
import formJs from "./regexDetail.js"; //新增弹框配置项
import CustomDialog from "@/components/materialQualityPage/rawMaterialManage/entryLedgerText/materialInfo/index.vue";
import publicForm1 from "@/components/public/el-form.vue"; //弹框form
import {
    getBaseDepartmentUser, //选择人
    saveEntryledger, //保存
    getEntryledgerByFid, //详情
    getSpeciesDataList, //种类
    getStoragelocationByCompanyId, //存放位置
    getPurveyorDataList,
    getSpecificationDataList
} from "@/api/request.js";
export default {
    props: [
        "dialogVisible",
        "dialogTitle",
        "statusNum",
        "currentRow",
        "fCompanyid",
        "fCompanyname"
    ],
    data() {
        return {
            visible: "",
            loading: false,
            title: null,
            isLock: false,
            positionData: [],
            positionData1: [],
            selected: [], //多选人
            positionObj: [],
            dialogHeight: null,
            formObj: formJs.formObj,
            additionalObj: formJs.additionalObj,
            isEdit: false,
            treeData: [], //材料种类
            formData: {
                fId: "", //传fid表示修改 不传表示新增
                userId: "", //当前登录人id
                fDetecttype: "", //材料检测类型
                userName: "", //当前登录人姓名
                recordfiles: [], //删除文件id集合
                fCompanyid: "", //标段id
                fCompanyname: "", //标段名称
                fProjectid: "", //项目id
                fProjectname: "", //项目名称
                fMaterialtypeid: "", //材料类别id
                fMaterialtypename: "", //材料类别名称
                fMaterialname: "", //材料名称
                fMaterialid: "", //材料id
                fEntrynumber: "", //进场编号
                fSpecification: "", //规格型号
                fUnit: "", //单位
                fInsfrequency: "", //检测频率
                fBasedstandard: "", //依据标准
                fSuppliername: "", //供应商名称
                fSupplierid: "", //供应商id
                fManufacturername: "", //生产厂家
                fManufacturerid: "", //生产厂家id
                fBatchnumber: "", //批号
                fEntrynum: "", //进场数量
                fEntrydate: "",
                fEntrydates: this.common.day(0), //进场日期
                fInspectionnum: "", //应检次数
                fLocationname: "", //存放位置名称集合 多位置用,隔开
                fLocationid: "", //存放位置id集合 多位置用,隔开
                fLocationnum: "", //存放数量集合 多位置用,隔开
                fIntendeduse: "", //拟用部位
                fReportid: "", //操作人id
                fReportname: "", //操作人姓名
                fFileurl: "", //资料
                fStatus: "", //状态
                fNote: "", //备注
                fLjuserid: "", //LJ单位抄送人id 多人用逗号隔开
                fLjusername: "", //LJ单位抄送人姓名 多人用逗号隔开
                fJluserid: "", //JL单位抄送人id 多人用逗号隔开
                fJlusername: "", //JL单位抄送人姓名 多人用逗号隔开
                fYzuserid: "", //业主单位抄送人id 多人用逗号隔开
                fYzusername: "", //业主单位抄送人姓名 多人用逗号隔开
                fLjusername1: [], //LJ单位抄送人姓名 多人用逗号隔开
                fJlusername1: [], //JL单位抄送人姓名 多人用逗号隔开
                fYzusername1: [] //业主单位抄送人姓名 多人用逗号隔开
            },
            formDatafileList: [], //工作计划资料
            btnShow: true,
            currentDetail: null,
            selectPostionObj: [],
            types: [],
            // 材料弹框
            materialRow: {},
            materialShow: false,
            fCompanynames: null,
            fCompanyids: null,
            fFileurllist: [], //附件
        };
    },
    components: {
        publicForm,
        publicForm1,
        CustomDialog
    },
    watch: {
        dialogVisible: {
            handler(newVal) {
                if (newVal) {
                    this.visible = newVal;
                }
            },
            immediate: true,
        },
        dialogTitle: {
            handler(newVal) {
                if (newVal) {
                    this.title = newVal;
                }
            },
            immediate: true,
        },
        statusNum: {
            handler(newVal) {
                if (newVal) {
                    this.edit(newVal);
                }
            },
            immediate: true,
        },

        currentRow: {
            handler(newVal) {
                if (newVal) {
                    this.currentDetail = newVal;
                    if (this.statusNum != 1) {
                        this.getDetail({
                            fId: this.currentDetail.fId
                        }) 
                    }
                }
            },
            immediate: true,
        },
        fCompanyid: {
            handler(newVal) {
                if (newVal) {
                    this.fCompanyids = newVal
                    this.getPosition({
                        fCompanyid: newVal
                    })
                }
            },
            immediate: true,
        },
        fCompanyname: {
            handler(newVal) {
                if (newVal) {
                    this.fCompanynames = newVal
                }
            },
            immediate: true,
        },

    },
    created() {
        window.addEventListener("resize", this.getHeight);
        this.getHeight();
    }, //获取页面高度自适应：

    destroyed() {
        window.removeEventListener("resize", this.getHeight);
        this.getHeight();
    },
    mounted() {
        this.userInfo = JSON.parse(sessionStorage.getItem("yjSmart-userInfo"));
        this.getPerson(this.userInfo.companyId, 0) //项目部
        this.getPerson(this.userInfo.jlCompanyId, 1) //监理单位
        this.getPerson(this.userInfo.projectId, 2) //业主

        this.btnShow = true
        this.getSpeciesList() //获取种类材料
        if (this.statusNum == 1) { //新增
            this.formData = this.$options.data.call(this).formData;
            this.btnShow = true
            this.getDetail({
                fCompanyid: this.userInfo.companyId,
                projectId: this.userInfo.projectId
            })
            this.formObj.map((item) => {
                item.disabled = true;
            });
            this.formObj[0].disabled = false;
            this.formObj[2].options = []
            this.formObj[2].disabled = false;
            this.formObj[6].disabled = false;
            this.formObj[7].disabled = false;
            this.formObj[8].disabled = false;
            this.formObj[10].disabled = false;
            this.isEdit = false;
            this.additionalObj.map((item) => {
                item.disabled = false;
            });
            this.additionalObj[0].uploadSelect = true
        } else if (this.statusNum == 2) { //编辑
            this.formData.fId = this.currentDetail.fId //按钮 
            this.getDetail({
                fId: this.currentDetail.fId
            })
            this.formObj.map((item) => {
                item.disabled = true;
            });
            this.formObj[0].disabled = false;
            this.formObj[2].disabled = false;
            this.formObj[6].disabled = false;
            this.formObj[7].disabled = false;
            this.formObj[8].disabled = false;
            this.formObj[10].disabled = false;
            this.isEdit = false;
            this.additionalObj.map((item) => {
                item.disabled = false;
            });
            this.additionalObj[0].uploadSelect = true
        } else if (this.statusNum == 7) { //查看
            this.btnShow = false
            this.formObj.map((item) => {
                item.disabled = true;
            });
            this.isEdit = true
            this.additionalObj.map((item) => {
                item.disabled = true;
            });
            this.additionalObj[0].uploadSelect = false
        }
        this.getHeight();
    },
    methods: {
        getHeight() {
            var h = document.documentElement.clientHeight; //获取页面可见高度
            this.dialogHeight = h * 0.76 + "px";
        },
        handleSelect(key) {
            this.activeIndex = key;
        },
        closeDialog() {
            this.$emit("close");
        },
        selectChange(data, a, b) {
            if (a == "fMaterialname") {
                this.materialShow = true
            } else if (b == "fManufacturername") { //生产厂家
                this.formData.fManufacturername = data.label; //生产厂家
                this.formData.fManufacturerid = data.value //生产厂家id
            } else if (b == "fSuppliername") { //供应商名称
                this.formData.fSuppliername = data.label; //供应商
                this.formData.fSupplierid = data.value //供应商id
            } else if (b == 'fSpecification') {
                this.formData.fSpecification = data.value //供应商id
            }
        },
        getMateral(data, types) { //获取材料信息
            this.formData.fMaterialtypeid = data.fVarietyid //材料种类id
            this.formData.fMaterialtypename = data.fVarietyname //材料种类
            this.formData.fMaterialid = data.fMaterialid //材料种类id
            this.formData.fMaterialname = data.fMaterialname //材料id
            this.formData.fSpecification = data.fVersion //规格型号
            this.formData.fUnit = data.fUnit //单位
            this.formData.fInsfrequency = data.fFrequency //检测频率
            this.formData.fBasedstandard = data.fStandard //依据标准
            this.formData.fDetecttype = types.fDetecttype //检测类型
            this.formData.fSuppliername = ""
            this.formData.fSupplierid = ""
            this.formData.fManufacturername = ""
            this.formData.fManufacturerid = ""
            this.getPurveyorDataList({
                id: data.fVarietyid, //种类id
                typ: 1
            })
            this.getPurveyorDataList({
                id: data.fVarietyid, //种类id
                typ: 2
            })
            this.yj_searchForm(data.fMaterialid);
            this.count(this.positionData)
        },
        // 获取进场数量
        getLocationnum() {
            var num;
            var fEntrynum = 0
            this.positionData.map(item => {

                if (item.fLocationnum != "") {
                    num = item.fLocationnum
                } else {
                    num = 0
                }
                fEntrynum += (num - 0)
            })
            // 应检次数
            if (this.formData.fInsfrequency != 0) {
                this.formData.fInspectionnum = Math.ceil(fEntrynum / this.formData.fInsfrequency)

            }
            this.formData.fEntrynum = fEntrynum
        },
        // 存放位置
        addUnqualifiedInfo() {
            this.positionData.push({
                fLocationname: "",
                fLocationid: "",
                fLocationnum: "",
            });
            this.positionData1.push({
                fLocationname: "",
                fLocationid: "",
                fLocationnum: "",
            });
        },
        // 删除存放位置
        deleteUnqualifiedInfo(index, row) {
            this.positionObj.map(item => {
                if (item.fId == row.fLocationname.fId) {
                    item.disabled = false
                } else if (item.fId == row.fLocationid) {
                    item.disabled = false
                }
            })
            this.positionData.splice(index, 1);
            this.positionData1.splice(index, 1);
            this.count(this.positionData)
        },
        count(data) {
            var fEntrynum = 0;
            var num;
            data.map(item => {
                if (item.fLocationnum != "") {
                    num = item.fLocationnum
                } else {
                    num = 0
                }
                fEntrynum += (num - 0)
            })
            // 应检次数
            if (this.formData.fInsfrequency != 0) {
                this.formData.fInspectionnum = Math.ceil(fEntrynum / this.formData.fInsfrequency)
            }
            this.formData.fEntrynum = fEntrynum
        },
        //存放位置选择
        selectPostion(row, name) { //存放位置选择
            console.log(name);
            var id = []
            this.positionData.map(item => {
                if (item.fLocationname != "") {
                    id.push(item.fLocationname.fId || item.fLocationid)
                }
            })
            this.positionObj.map(item => {
                item.disabled = false
                id.map(i => {
                    if (item.fId == i) {
                        item.disabled = true
                    }
                })
            })
        },
        //存放位置获取
        getPosition(obj) { //存放位置获取
            getStoragelocationByCompanyId(obj).then(res => {
                if (res.errcode == 0) {
                    res.data.map(item => {
                        item.disabled = false
                    })
                    this.positionObj = res.data
                }
            })
        },
        edit() {
            this.formData = this.$options.data.call(this).formData;
        },
        getPurveyorDataList(data) { //材料生产厂商
            var obj = {
                page: 1,
                pageSize: 20,
                merchantName: "", //生产厂家
                fVarietyid: data.id, //种类名
                typ: data.typ //"1"材料生产商，"2"材料供应商
            }
            this.formObj[6].options = []
            this.formObj[7].options = []

            getPurveyorDataList(obj)
                .then((res) => {
                    if (res.errcode == 0) {
                        var info = []
                        res.data.pageInfo.list.map(item => {
                            info.push({
                                label: item.fMerchantname,
                                value: item.fId
                            })
                        })
                        if (data.typ == 1) {
                            this.formObj[7].options = info;

                        } else if (data.typ == 2) {
                            this.formObj[6].options = info;

                        }

                    } else {
                        this.$message.error("列表数据获取失败", res.msg);
                        this.loading = false;
                    }
                })
        },
        // 材料种类
        getSpeciesList() {
            var obj = {
                page: 1,
                pageSize: 500
            }
            getSpeciesDataList(obj)
                .then((res) => {
                    if (res.errcode == 0) {
                        this.treeData = []
                        this.formObj[0].options = []
                        res.data.pageInfo.list.map(item => {
                            if (item.fStatus == 1) {
                                this.formObj[0].options.push({
                                    label: item.fVarietyname,
                                    value: item.fVarietyid,
                                    children: item.materialList
                                })
                            }
                        })

                    } else {
                        this.$message.error(res.msg);
                        return false;
                    }
                })
                .catch(() => {
                    return false;
                });
        },
        getPerson(id, status) {
            var obj = {
                fCompanyid: id,
            };
            getBaseDepartmentUser(obj)
                .then((res) => {
                    if (res.errcode == 0) {

                        var arr = res.data.childList[0].personList;
                        var personList = [];

                        arr.map((item) => {
                            personList.push({
                                label: item.fRealname,
                                value: item.fUserid,
                            });

                        });
                        if (status == 0) {
                            this.additionalObj[1].options = personList;

                        } else if (status == 1) {
                            this.additionalObj[2].options = personList;

                        } else if (status == 2) {
                            this.additionalObj[3].options = personList;

                        }

                    } else {
                        this.$message.error(res.msg);
                        return false;
                    }
                })

        },
        // 刪除文件
        delteFile(file) {
            for (let index = 0; index < this.fFileurllist.length; index++) {
                if (file.uid == this.fFileurllist[index].uid) {
                    if (this.fFileurllist[index].atta == undefined) {
                        this.formData.recordfiles.push({
                            fId: this.fFileurllist[index].fId,
                        });
                    } else {
                        this.formData.recordfiles.push({
                            fId: this.fFileurllist[index].atta.fId,
                        });
                    }
                    this.fFileurllist.splice(index, 1);
                }
            }
        },
        // 详情
        getDetail(obj) {
            this.loading = true;
            getEntryledgerByFid(obj).then((res) => {
                if (res.errcode == 0) {
                    this.loading = false;
                    var data = res.data.yjQmpClEntryledger
                    this.formData = data;
                    this.formData.fFileurl = data.fFileurl
                    if (obj.fCompanyid) {
                        this.formData.fCompanyname = this.userInfo.companyFullName; //当前登录人标段名称
                        this.formData.fCompanyid = this.userInfo.companyId; //当前登录人标段ID
                    }
                    if (data.fEntrydates == null || data.fEntrydates == "") {
                        this.formData.fEntrydates = this.common.day(0)
                        this.formData.fLjusername1 = [] //LJ单位抄送人姓名 多人用逗号隔开
                        this.formData.fJlusername1 = [] //JL单位抄送人姓名 多人用逗号隔开
                        this.formData.fYzusername1 = [] //业主单位抄送人姓名
                    } else {
                        this.formData.recordfiles = []
                        this.fFileurllist = res.data.fFileurllist
                        this.getPurveyorDataList({
                            id: data.fMaterialtypeid, //种类id
                            typ: 1
                        })

                        this.getPurveyorDataList({

                            id: data.fMaterialtypeid, //种类id
                            typ: 2
                        })
                        this.yj_searchForm(data.fMaterialid)

                        var fLjusername1 = [] //项目部
                        if (data.fLjuserid != '') {
                            var id1 = data.fLjuserid.split(",")
                            var name1 = data.fLjusername.split(",")
                            id1.map((item, index) => {
                                fLjusername1.push({
                                    label: name1[index],
                                    value: item
                                })
                            })
                        } else {
                            fLjusername1 = []
                        }


                        var fJlusername1 = [] //监理
                        if (data.fJluserid != '') {
                            var id2 = data.fJluserid.split(",")
                            var name2 = data.fJlusername.split(",")
                            id2.map((item, index) => {
                                fJlusername1.push({
                                    label: name2[index],
                                    value: item
                                })
                            })

                        } else {
                            fJlusername1 = []
                        }

                        var fYzusername1 = [] //业主
                        if (data.fYzuserid != '') {
                            var id3 = data.fYzuserid.split(",")
                            var name3 = data.fYzusername.split(",")
                            id3.map((item, index) => {
                                fYzusername1.push({
                                    label: name3[index],
                                    value: item
                                })
                            })
                        } else {
                            fYzusername1 = []
                        }
                        this.selected = [{
                            name: "fLjusername1",
                            selected: fLjusername1
                        },
                        {
                            name: "fJlusername1",
                            selected: fJlusername1
                        }, {
                            name: "fYzusername1",
                            selected: fYzusername1
                        }
                        ]
                    }
                    if (data.fLocationname != '') {
                        var Locationname = data.fLocationname.split(",")
                        var fLocationnum = data.fLocationnum.split(",")
                        var fLocationid = data.fLocationid.split(",")
                        this.positionData = []
                        fLocationid.map((item, index) => {
                            this.positionData.push({
                                fLocationname: Locationname[index],
                                fLocationid: item,
                                fLocationnum: fLocationnum[index],
                            })
                        })
                        this.positionObj.map(item => {
                            fLocationid.map(i => {
                                if (item.fId == i) {
                                    item.disabled = true
                                }
                            })
                        })
                    }
                    if (this.statusNum == 7) {
                        this.btnShow = false;
                    } else {
                        this.btnShow = true;
                    }


                } else {
                    this.loading = false;
                    this.$message.error(res.msg);
                }
            });
        },
        yj_searchForm(data) {
            var obj = {
                page: 1,
                pageSize: 500,
                fId: data
            }
            this.formObj[2].options = [];
            getSpecificationDataList(obj)
                .then((res) => {
                    if (res.errcode == 0) {
                        var data1 = []
                        res.data.pageInfo.list.map(item => {
                            data1.push({
                                label: item.fVersion,
                                value: item.fVersion,
                            })
                        })
                        this.formObj[2].options = data1;
                        // this.$message.error("列表数据获取失败", res.msg);
                        this.loading = false;
                    }
                })
        },
        yj_saveForm() {
            this.userInfo = JSON.parse(sessionStorage.getItem("yjSmart-userInfo"));
            this.formData.fReportid = this.userInfo.userId; //当前登录人id
            this.formData.fReportname = this.userInfo.realName; //当前登录人名称
            this.formData.userId = this.userInfo.userId; //当前登录人id
            this.formData.userName = this.userInfo.realName; //当前登录人名称
            this.formData.fProjectid = this.userInfo.projectId; //项目名称id
            this.formData.fProjectname = this.userInfo.projectName; //项目名称
            //项目部
            var name1 = [];
            var id1 = [];
            this.formData.fLjusername1.map(item => {
                name1.push(item.label)
                id1.push(item.value)
            })
            this.formData.fLjuserid = id1.join(",")
            this.formData.fLjusername = name1.join(",")
            //项目部
            //监理单位
            var name2 = [];
            var id2 = [];
            this.formData.fJlusername1.map(item => {
                name2.push(item.label)
                id2.push(item.value)
            })
            this.formData.fJluserid = id2.join(",")
            this.formData.fJlusername = name2.join(",")
            //监理单位
            //业主
            var name3 = [];
            var id3 = [];
            this.formData.fYzusername1.map(item => {
                name3.push(item.label)
                id3.push(item.value)
            })
            this.formData.fYzuserid = id3.join(",")
            this.formData.fYzusername = name3.join(",")
            //业主
            // 存放位置
            var position = []
            var positionId = []
            var positionNum = []
            this.positionData.map(item => {

                if (item.fLocationname != "") {
                    position.push(item.fLocationname.fLocationname || item.fLocationname)
                    positionId.push(item.fLocationname.fId || item.fLocationid)
                    positionNum.push(item.fLocationnum)
                }
            })
            this.formData.fLocationname = position.join(",") //位置
            this.formData.fLocationid = positionId.join(",") //位置id
            this.formData.fLocationnum = positionNum.join(",") //位置数量

            // 存放位置

            // 0连续新增1新增
            if (this.$refs.publicForm.submitForm()) {
                this.saveForm(this.formData);
            } else {
                console.log("error");
            }

        },
        // 保存
        saveForm(data) {
            this.loading = true;
            this.isLock = true
            saveEntryledger(data)
                .then((res) => {
                    if (res.errcode == 0) {
                        this.loading = false;
                        this.visible = false;
                        this.$emit("refreshList");
                        this.$message.success("新增成功");
                        this.isLock = false
                    } else {
                        this.loading = false;
                        this.isLock = false
                        this.$message.error(res.msg);
                        console.log("接口报错");
                    }
                })
                .catch((error) => {
                    console.log("拌合站设置-新增）", error);
                });

        },
        refreshList() { },
        materialDialog() {
            this.materialShow = false;
        },
    },
};